#ifndef _CQUADTREE_H
#define _CQUADTREE_H

#include "CObject.h"
#include <vector>

class CQuadTreeNode;
//---------------------------------------//
//			Class QuadTree				 //
//---------------------------------------//
class CQuadTree
{
public:
	//--------------------------------------------//
	//	Constructor and Destructor for this class //
	//--------------------------------------------//
	CQuadTree( ULONG maxLevel , long width , long height);
	virtual ~CQuadTree();

	//--------------------------------//
	//	Public methods for this class //
	//--------------------------------//
	HRESULT		SubDivide		( CQuadTreeNode* pNode );
	HRESULT		AddObject		( CObject* object , CQuadTreeNode* pNode = NULL );
	HRESULT		GetObject		( RECT rect , std::vector<CObject*> &listObject , CQuadTreeNode* pNode = NULL );
	HRESULT		RemoveObject	( CObject* object , CQuadTreeNode* pNode = NULL );

	void		UpdateNode		( CObject* object );

private:
	//-----------------------------------//
	//	Private variables for this class //
	//-----------------------------------//
	long				m_nWidth;
	long				m_nHeight;
	ULONG				m_nMaxLevel;
	CQuadTreeNode*		m_pRootNode;

	//---------------------------------//
	//	Private methods for this class //
	//---------------------------------//
	bool		isCollision			( RECT objRect , RECT nodeRect );
	RECT		CreateVirtualNode	( long upperLeftX , long upperLeftY , long width , long height );
};



//---------------------------------------//
//			Class QuadTreeNode           //
//---------------------------------------//
class CQuadTreeNode
{
public:
	//--------------------------------------------//
	//	Constructor and Destructor for this class //
	//--------------------------------------------//
	CQuadTreeNode( CQuadTreeNode* pParentNode , long upperLeftX , long upperLeftY , long width , long height );
	virtual ~CQuadTreeNode();

	//--------------------------------//
	//	Public methods for this class //
	//--------------------------------//
	void		AddObject				( CObject* object );
	void		SetLevel				( ULONG lev );
	HRESULT		GetObject				( std::vector<CObject*> &listObject );
	HRESULT		RemoveObject			( CObject* object );
	RECT		GetSize					( );
	long		GetWidth				( );
	long		GetHeight				( );
	long		GetPosX					( );
	long		GetPosY					( );
	ULONG		GetLevel				( );


	//----------------------------------//
	//	Public variables for this class //
	//----------------------------------//
	CQuadTreeNode**			m_pSubNode;
	CQuadTreeNode*			m_pParentNode;

private:
	//-----------------------------------//
	//	Private variables for this class //
	//-----------------------------------//
	ULONG					m_nNodeLevel;
	long					m_nUpperLeftX;
	long					m_nUpperLeftY;
	long					m_nWidth;
	long					m_nHeight;

	std::vector<CObject*>	m_listObject;
	
};



//class CQuadTreeNode
//{
//public:
//	//--------------------------------------------//
//	//	Constructor and Destructor for this class //
//	//--------------------------------------------//
//	CQuadTreeNode(long upperLeftX , long upperLeftY , long width , long height);
//	~CQuadTreeNode();
//	
//	//-------------------------------//
//	// Public methods for this class //
//	//-------------------------------//
//	HRESULT		SubDivine			();
//	HRESULT		AddObject			( IColliableObject *object );
//	HRESULT		GetObject			( IColliableObject *object , std::vector<IColliableObject*> &listObject );
//	HRESULT		RemoveObject		( IColliableObject *object );
//	void		Update				( IColliableObject *Object );
//	void		GetSize				( long *upperLeftX , long *upperLeftY , long *width , long *height );
//private:
//	//-----------------------------------//
//	//	Private variables for this class //
//	//-----------------------------------//
//	long								m_UpperLeftX;
//	long								m_UpperLeftY;
//	long								m_Width;
//	long								m_Height;
//
//	CQuadTreeNode**						m_ppSubNode;
//
//	std::vector<IColliableObject*>		m_ListObject;
//};

#endif